/*
 * Copyright (c) 1999-2003, Carnegie Mellon University. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Products derived from the software may not be called "iVProg",
 *    nor may "iVProg" appear in their name, without prior written
 *    permission of Carnegie Mellon University.
 *
 * 4. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes software developed by Carnegie Mellon University"
 */

package edu.cmu.cs.stage3.alice.core;


public class World extends ReferenceFrame {

private static int staticCont=0;
public int cont=staticCont++;

 private static boolean HACK_s_isPropetryListeningDisabledWhileWorldIsRunning = false;
 static {
  System.out.println("World.java: static: #"+staticCont);//
  try { // System.out.println("World.java: static OK");//
   if (!edu.cmu.cs.stage3.alice.authoringtool.JAlice.isApplet())
      HACK_s_isPropetryListeningDisabledWhileWorldIsRunning = Boolean.getBoolean("alice.isPropetryListeningDisabledWhileWorldIsRunning");
   else
      HACK_s_isPropetryListeningDisabledWhileWorldIsRunning = false;
  } catch (Throwable t) { // System.out.println("World.java: static ERRO -------");//
   HACK_s_isPropetryListeningDisabledWhileWorldIsRunning = false;
   t.printStackTrace();
   }
  }

 private java.util.Vector m_capsulePropertyValuePairs = new java.util.Vector();
 private java.util.Hashtable m_capsuleElements = new java.util.Hashtable();

 private edu.cmu.cs.stage3.alice.scripting.Interpreter m_interpreter;

 private java.util.Vector m_messageListeners = new java.util.Vector();
 private edu.cmu.cs.stage3.alice.core.event.MessageListener[] m_messageListenerArray = null;

 private Sandbox m_currentSandbox = null;

 private Clock m_clock = null;
 private boolean m_isRunning = false;

 // Came from: edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool.worldInit: private void worldInit(): world = new World();
 public World () {
  super();
  System.out.println("World.java: World(): #"+this.cont);//
  // try{String _str="";System.out.println(_str.charAt(3));}catch(Exception e){e.printStackTrace();}
  }

 public Clock getClock () {
  return m_clock;
  }

 public void setClock (Clock clock) {
  m_clock = clock;
  }

 private edu.cmu.cs.stage3.alice.scripting.Interpreter getInterpreter () {
  return m_interpreter;
  }

 //Rem @Override
 public edu.cmu.cs.stage3.alice.scripting.Code compile (Object source, edu.cmu.cs.stage3.alice.scripting.CompileType compileType) {
  return getInterpreter().compile(source, compileType);
  }

 //Rem @Override
 public Object eval (edu.cmu.cs.stage3.alice.scripting.Code code) {
  return getInterpreter().eval(code);
  }

 //Rem @Override
 public void exec (edu.cmu.cs.stage3.alice.scripting.Code code) {
  getInterpreter().exec(code);
  }

 //Rem @Override
 protected void internalRelease (int pass) { }

 //Rem @Override
 protected void nameValueChanged (String value) { }

 //Rem @Override
 protected void propertyChanged (Property property, Object value) { }

 public void sendMessage (Element source, String message, Transformable fromWho, Transformable toWhom, long when) {
  edu.cmu.cs.stage3.alice.core.event.MessageEvent messageEvent = new edu.cmu.cs.stage3.alice.core.event.MessageEvent( source, message, fromWho, toWhom, when );
  for (int i=0; i<m_messageListeners.size(); i++) {
   edu.cmu.cs.stage3.alice.core.event.MessageListener messageListener = (edu.cmu.cs.stage3.alice.core.event.MessageListener)m_messageListeners.elementAt( i );
   messageListener.messageSent( messageEvent );
   }
  }

 public void addMessageListener (edu.cmu.cs.stage3.alice.core.event.MessageListener messageListener) {
  m_messageListeners.addElement( messageListener );
  m_messageListenerArray = null;
  }

 public void removeMessageListener (edu.cmu.cs.stage3.alice.core.event.MessageListener messageListener) {
  m_messageListeners.removeElement( messageListener );
  m_messageListenerArray = null;
  }

 public edu.cmu.cs.stage3.alice.core.event.MessageListener[] getMessageListeners () {
  if (m_messageListenerArray==null ) {
   m_messageListenerArray = new edu.cmu.cs.stage3.alice.core.event.MessageListener[m_messageListeners.size()];
   m_messageListeners.copyInto( m_messageListenerArray );
   }
  return m_messageListenerArray;
  }

 public void preserve () {
  m_capsulePropertyValuePairs.clear();
  m_capsuleElements.clear();
  Element[] elements = getDescendants();
  //LOB for (Element element : elements)
  int t_tam = elements!=null ? elements.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   m_capsuleElements.put( elements[i_i], Boolean.TRUE ); //L element, Boolean.TRUE
   Property[] properties = elements[i_i].getProperties(); //L element.getProperties()
   //LOB for (Property property : properties)
   int tt_tam = properties!=null ? properties.length : 0;
   for (int i_j=0; i_j<tt_tam; i_j++) {
    Object[] tuple = { properties[i_j], properties[i_j].get() }; //L property, property.get()
    m_capsulePropertyValuePairs.addElement( tuple );
    }
   }
  }

 public void restore () {
  java.util.Enumeration preserves = m_capsulePropertyValuePairs.elements();
  while( preserves.hasMoreElements() ) {
   Object[] tuple = (Object[])preserves.nextElement();
   Property property = (Property)tuple[0];
   Object value = tuple[1];
   property.set( value );
   }
  Element[] elements = getDescendants();
  //LOB for (Element element : elements)
  int t_tam = elements!=null ? elements.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   if (m_capsuleElements.get( elements[i_i] )==null ) { //L element
      elements[i_i].removeFromParent(); //L elements.removeFromParent();
      }
  //LOB }
  //LOB for (Element element : elements)
   if (m_capsuleElements.get( elements[i_i] )==null ) { //L  element
      elements[i_i].release(); //L elements.release();
      }
   }
  }

 public Sandbox getCurrentSandbox () {
  return m_currentSandbox;
  }

 public boolean isRunning () {
  return m_isRunning;
  }

 public void start () {
  // System.out.println("World.java: start(): "+HACK_s_isPropetryListeningDisabledWhileWorldIsRunning);//
  if (HACK_s_isPropetryListeningDisabledWhileWorldIsRunning) {
   Property.HACK_disableListening();
   }
  started( this, m_clock.getTime() );
  m_isRunning = true;
  }

 public void stop () {
  m_isRunning = false;
  stopped( this, m_clock.getTime() );
  if (HACK_s_isPropetryListeningDisabledWhileWorldIsRunning) {
   Property.HACK_enableListening();
   }
  }

 // To execute instructions
 // From: edu.cmu.cs.stage3.alice.core.clock.DefaultClock.schedule():  void schedule(): 'updateTime(); m_world.schedule();'
 // To  : edu.cmu.cs.stage3.alice.core.Sandbox.scheduleBehaviors(Sandbox.java:59)
 public void schedule () {
  m_currentSandbox = this;
  scheduleBehaviors( m_clock.getTime() ); // to execute instructions
  m_currentSandbox = null;
  }

 }
